import 'package:flutter/material.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/widget/w_line.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

class TabBtn extends StatefulWidget {
  final List<Map> tabList;
  final String titleKey;
  final double height;
  final double? width;
  final Color backColor;
  final Color textColor;
  final Color selectTextColor;
  final double textSize;
  final double lineHeight;
  final double? lineWidth;
  final double lineRadius;
  final double paddingTop;
  final double paddingBottom;
  final int? selectedIndex;
  final Function(int index)? tabClick;

  const TabBtn(
      {super.key,
      required this.tabList,
      this.height = 28,
      this.width,
      this.titleKey = "title",
      this.backColor = WColors.white,
      this.textColor = WColors.mainTextColor,
      this.selectTextColor = WColors.white,
      this.paddingTop = 2,
      this.paddingBottom = 2,
      this.lineHeight = 3,
      this.lineWidth,
      this.lineRadius = 3,
      this.tabClick,
      this.selectedIndex = 0,
      this.textSize = 12});

  @override
  State<TabBtn> createState() => _TabBtnState();
}

class _TabBtnState extends State<TabBtn> {
  int currentIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentIndex = widget.selectedIndex ?? 0;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height.px,
      width: widget.width?.px ?? SizeUtils.screenW(),
      margin: EdgeInsets.only(
          left: 10.px,
          top: widget.paddingTop.px,
          bottom: widget.paddingBottom.px),
      alignment: Alignment.center,
      child: _tabList(),
    );
  }

  ListView _tabList() {
    List<Widget> list = [];
    for (int i = 0; i < widget.tabList.length; i++) {
      var item = widget.tabList[i];
      list.add(GestureDetector(
        onTap: () {
          currentIndex = i;
          setState(() {});
          if (widget.tabClick != null) {
            widget.tabClick!(i);
          }
        },
        child: Container(
            constraints: BoxConstraints(minWidth: SizeUtils.screenW() / 5),
            padding: EdgeInsets.symmetric(horizontal: 5.px),
            height: widget.height.px,
            margin: EdgeInsets.only(right: 5.px),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: currentIndex == i ? WColors.primaryColor : WColors.white,
                borderRadius: BorderRadius.circular(widget.height.px),
                border: Border.all(
                    color: currentIndex == i
                        ? WColors.primaryColor
                        : WColors.border,
                    width: 1.px)),
            child: WText(
              "${item[widget.titleKey]}${item["count"] != null ? '(${item["count"]})' : ''}",
              style: TextStyle(
                  color: currentIndex == i
                      ? widget.selectTextColor
                      : widget.textColor,
                  fontSize: widget.textSize.px),
            )),
      ));
    }
    return ListView(
      padding: EdgeInsets.zero,
      scrollDirection: Axis.horizontal,
      children: list,
    );
  }
}
